package com.ziyun.erp.common.api;

import com.alibaba.fastjson.JSONObject;
import com.ziyun.erp.common.e.accessconfig.AccessConfigEnum;
import com.ziyun.erp.common.e.supp.MerchantTypeEnum;
import com.ziyun.erp.common.utils.HttpClientUtil;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.systemConfig.service.ErpAccessConfigService;
import io.renren.common.utils.SpringContextUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 报价账号创建
 * @author gaosheng
 * @email
 * @date 2018-09-01 11:11
 */
public class PopRegisterApi {

	/**
	 * 日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(PopRegisterApi.class);

	/**
	 * 注册 报价平台账户
	 *
	 * @param sellerInfo
	 *            小B 图文店信息 orgType:ORGT5
	 *            工厂账户 orgType:ORGT4
	 * @return
	 */
	public static Map quoteMerchantsRegister(SellerInfoEntity sellerInfo) {
		logger.info("==============quote merchants register start===============");
		Map<String, String> map = new HashMap<>();
		map.put("orgName", sellerInfo.getSellerName());
		map.put("contacts", sellerInfo.getName());
		map.put("contactWay", sellerInfo.getSellerMobile());
		map.put("qq", sellerInfo.getSellerQq());
		map.put("address", sellerInfo.getAddr());
		map.put("desc", sellerInfo.getDescription());
		map.put("account", sellerInfo.getSellerName());
		map.put("erpId", sellerInfo.getSellerId().toString());
		map.put("password", "000000");
		map.put("areaId", sellerInfo.getAreaId());
		if (StringUtils.equals(sellerInfo.getSellerType(), MerchantTypeEnum.MERCHANT.toCode())){
			map.put("orgType", "ORGT5");
			map.put("role", "supplier");
		}else {
			map.put("orgType", "ORGT4");
			map.put("role", "factory");
		}
		return quoteRegister(map);
	}

	/**
	 * 注册 pop平台账户
	 *
	 * @param map
	 * @return
	 */
	private static Map quoteRegister(Map<String, String> map) {
		Map<String, String> postJson = new HashMap<>();
		// 组织名
		postJson.put("orgName", map.get("orgName"));
		// 组织类型
		postJson.put("orgType", map.get("orgType"));
		// 联系人
		postJson.put("contacts", map.get("contacts"));
		// 联系方式
		postJson.put("contactWay", map.get("contactWay"));
		// QQ号
		postJson.put("qq", map.get("qq"));
		// 地址
		postJson.put("address", map.get("address"));
		// 备注
		postJson.put("desc", map.get("desc"));
		// 登录账号
		postJson.put("account", map.get("account"));
		// 密码
		postJson.put("password", map.get("password"));
		// 角色
		postJson.put("role", map.get("role"));
		// 区域ID
		postJson.put("areaId", map.get("areaId"));
		// 商户ID 或是 工厂ID
		postJson.put("erpFactoryId", map.get("erpId"));
		logger.info("==============quote register param："+ postJson.toString());

		Map<String, Object> resultMap = new HashMap<>();
		try {
			ErpAccessConfigService erpAccessConfigService = SpringContextUtils.getBean("erpAccessConfigService", ErpAccessConfigService.class);
			String accessValue = erpAccessConfigService.queryByCode(AccessConfigEnum.POP_REGISTER.toCode());
			logger.info("quote register url：" + accessValue);
			String re = HttpClientUtil.doPost(accessValue, postJson);
			logger.info("quote register result：" + re);
			JSONObject result = (JSONObject) JSONObject.parse(re);
			String code = result.getString("code");
			if (StringUtils.equals("0", code)) {
				resultMap.put("result", true);
				resultMap.put("msg", "报价平台账号创建成功");
				return resultMap;
			} else if (StringUtils.equals("500", code)) {
				String msg = result.getString("msg");
				if (!msg.contains("服务器错误")) {
					resultMap.put("result", false);
					resultMap.put("msg", msg);
					return resultMap;
				}
			}
		} catch (Exception e) {
			logger.info("==============quote register exception：" + e.getMessage());
			e.printStackTrace();
		}
		resultMap.put("result", false);
		resultMap.put("msg", "报价平台账号创建失败");

        logger.info("==============quote register returnCode：" + resultMap.toString());
        logger.info("==============quote register end===============");
		return resultMap;
	}

}
